package com.cw.interceptor;

import com.cw.utils.Constants;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author cw
 * @since 2021/7/14 15:54
 * @deprecated 拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //若session不为空，则说明用户登录过且没注销，并且没有关闭过窗口
        HttpSession session = request.getSession();
        if (session.getAttribute(Constants.USER_SESSION) != null) {
            return true;
        }

        //登录页面也要放行
        if (request.getRequestURI().contains("login")) {
            return true;
        }

        //若用户没有登陆，则跳转到登陆页面
        response.sendRedirect("/login.jsp");
        return false;
    }

}

